package com.yanqu.road.server.cmd.activity.tongbing;

import com.yanqu.road.entity.union.activitygroup.UnionActivityGroup;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.logic.pb.UnionBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.TongBingProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.CrossUnionMgr;
import com.yanqu.road.server.manager.union.activitygroup.Cross3UnionActivityGroupMgr;
import com.yanqu.road.server.protocol.CrossProtocol;

import java.util.*;

@Cmd(code = CrossProtocol.C3_CROSS_TONG_BING_GET_UNION_REQ, desc = "")
public class Cross3TongBingGetUnionReqCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        TongBingProto.CrossTongBingGetUnionReqMsg reqMsg = TongBingProto.CrossTongBingGetUnionReqMsg.parseFrom(packet.getBytes());
        int activityId = reqMsg.getActivityId();
        long gameServerId = reqMsg.getGameServerId();
        int type = reqMsg.getType();
        int groupId = reqMsg.getGroupId();

        // 获取商会UID
        List<String> unionUid = null;
        if (type == 1) {
            unionUid = reqMsg.getUnionUidList();
        } else if (type == 2) {
            if (groupId == UnionActivityGroupHelper.NO_NEED_GROUP_ID) {
                // 旧版活动
                unionUid = getTopEarnSpeedUnion(reqMsg.getEarnSpeedTopCount(), new HashSet<>(reqMsg.getServerIdList()));
            } else {
                // 新版活动
                UnionActivityGroup groupData = Cross3UnionActivityGroupMgr.getGroupData(activityId, groupId);
                if (groupData != null && groupData.getUnionDataMap().size() > 0) {
                    unionUid = new ArrayList<>(groupData.getUnionDataMap().keySet());
                }
            }
        }
        if (unionUid == null || unionUid.isEmpty()) {
            return;
        }

        // 返回值
        TongBingProto.CrossTongBingGetUnionRespMsg.Builder builder = TongBingProto.CrossTongBingGetUnionRespMsg.newBuilder();
        builder.setActivityId(activityId);
        builder.setGameServerId(gameServerId);
        builder.setType(type);
        for (String s : unionUid) {
            CrossUnionInfo unionInfo = CrossUnionMgr.getUnionInfo(s);
            Map<Long, CrossUnionMember> unionMemberMap = CrossUnionMgr.getUnionMemberMap(s);
            if (unionInfo != null && unionMemberMap != null) {
                TongBingProto.TongBingUnionMsg.Builder tmp = TongBingProto.TongBingUnionMsg.newBuilder();
                tmp.setUnionUid(s);
                tmp.setUnionInfo(UnionBasePb.parseUnionBaseTempMsg(CrossUnionMgr.parseUnionBaseInfo(unionInfo)));
                for (CrossUnionMember member : unionMemberMap.values()) {
                    tmp.addMember(TongBingProto.TongBingUnionMemberInfoMsg.newBuilder()
                            .setUserId(member.getUserId())
                            .setServerId(member.getServerId())
                            .setPosition(member.getPosition())
                    );
                }
                builder.addUnion(tmp);
            }
        }

        // 返回
        MessageHelper.sendUnionActivityGroupPacket(gameServerId, 0, CrossProtocol.C2_CROSS_TONG_BING_GET_UNION_RESP, builder, reqMsg.getActivityId(),
                -1, groupId);
    }

    @Override
    public void sendData() {

    }


    private List<String> getTopEarnSpeedUnion(int count, Set<Long> serverIdList) {
        // 获取活动范围内全部商会
        List<CrossUnionInfo> rankList = CrossUnionMgr.getUnionListByServerList(serverIdList);
        // 排序
        rankList.sort(new Comparator<CrossUnionInfo>() {
            @Override
            public int compare(CrossUnionInfo o1, CrossUnionInfo o2) {
                return o2.getTotalEarnSpeed().compareTo(o1.getTotalEarnSpeed());
            }
        });
        // 获取前几名
        List<String> list = new ArrayList<>();
        int rank = 0;
        for (CrossUnionInfo crossUnionInfo : rankList) {
            if (rank >= count) {
                break;
            }
            rank++;
            list.add(crossUnionInfo.getUnionUid());
        }
        return list;
    }

}
